package com.wfh.activity;

import java.util.ArrayList;

import com.wfh.R;
import com.wfh.adapter.PhoneAdapter;
import com.wfh.util.Util;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class InteractActivity extends CommonActivity {

	private TextView phone, nav, share,login;
	private Dialog alertdialog;
	private BroadcastReceiver mReceiver = new DataReceiver();
	private ArrayList<String> phonelist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState, R.layout.activity_interact);

		phone = (TextView) findViewById(R.id.phone);
		nav = (TextView) findViewById(R.id.nav);
		share = (TextView) findViewById(R.id.share);
		login=(TextView)findViewById(R.id.login);
		
		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_PHONE);
		registerReceiver(mReceiver, filter);

		phone.setOnClickListener(phoneListener);
		nav.setOnClickListener(navListener);
		login.setOnClickListener(loginListener);
		share.setOnClickListener(shareListener);

	}

	//电话
	OnClickListener phoneListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			HomeActivity.service.getPhoneList(InteractActivity.this, null);
		}
	};

	//导航
	OnClickListener navListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Util.startactivity(InteractActivity.this, MapActivity.class);
		}
	};
	
	
	//登录
	
	OnClickListener loginListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Util.startactivity(InteractActivity.this, LoginActivity.class);
		}
	};
	
	
	//分享
	
	OnClickListener shareListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Util.startactivity(InteractActivity.this,ShareListActivity.class);
		}
	};
	
	// 接收器
	private class DataReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Util.D_PHONE)) {
				Bundle bd = intent.getExtras();
				phonelist = bd.getStringArrayList("phone");
				if(phonelist==null || phonelist.size()<0) {
					Util.showToast(InteractActivity.this, "该店铺没有号码");
					return;
				}
				ListView list = new ListView(InteractActivity.this);
				list.setAdapter(new PhoneAdapter(InteractActivity.this,
						phonelist));
				alertdialog = Util.alertDialog(InteractActivity.this, list,
						"拨打店铺号码", null);
				alertdialog.show();
				list.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Intent intent = new Intent(Intent.ACTION_DIAL, Uri
								.parse("tel:" + phonelist.get(position)));
						startActivity(intent);
						alertdialog.hide();
					}
				});
			}

		}
	}
	
	

	@Override
	protected void onDestroy() {
		if(alertdialog!=null) alertdialog.cancel();
		unregisterReceiver(mReceiver);
		super.onDestroy();
		
	}

	// 监听返回键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "退出");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			android.os.Process.killProcess(android.os.Process.myPid());
			System.gc();
			System.exit(0);
			notify();
			break;
		default:
			break;
		}
		return true;
	}

}
